package com.piece.admin.controller.oauth;

import com.piece.admin.model.TbSysUserOauth;
import com.piece.admin.service.TbSysUserOauthService;
import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.web.controller.FrameWorkController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = {"第三方账号绑定管理"})
@RequestMapping("/api/system/oauth/user")
public class SysUserOauthController extends FrameWorkController {

    @Resource
    private TbSysUserOauthService tbSysUserOauthService;


    @PostMapping("/list")
    @AclPermission(code = "system:oauth:view")
    @ApiOperation(value = "查询第三方账号列表")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public List list() {
        QueryTable queryTable = initTable();
        DataTable dataTable = tbSysUserOauthService.findPage(queryTable);
        return dataTable.getData();
    }

    @PostMapping("/table")
    @AclPermission(code = "system:oauth:view")
    @ApiOperation(value = "查询第三方账号分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse table() {
        QueryTable queryTable = initTable();
        DataTable dataTable = tbSysUserOauthService.findPage(queryTable);
        return success(dataTable);
    }

    @GetMapping("/load")
    @AclPermission(code = "system:oauth:view")
    @ApiOperation(value = "加载第三方账号表单数据")
    @ApiImplicitParam(name="id", value = "主键")
    public AjaxResponse load() {
        long id = Convert.toLong(getRequest().getParameter("id"), 0L);
        TbSysUserOauth tbSysUserOauth = new TbSysUserOauth();
        if (id != 0) {
            tbSysUserOauth = tbSysUserOauthService.findById(id);
        }
        return success(tbSysUserOauth);
    }

    @SubmitPermission
    @PostMapping("/save")
    @AclPermission(code = "system:oauth:add,system:oauth:edit")
    @ApiOperation(value = "保存第三方账号表单数据")
    @ApiImplicitParam(value = "第三方账号数据", dataTypeClass = TbSysUserOauth.class, type = "body")
    public AjaxResponse save() {
        TbSysUserOauth tbSysUserOauth;
        try {
            Long id = Convert.toLong(getRequest().getParameter("id"));
            if (null != id) {
                tbSysUserOauth = tbSysUserOauthService.findById(id);
            } else {
                tbSysUserOauth = new TbSysUserOauth();
            }
            initDB(tbSysUserOauth);
            tbSysUserOauthService.insertOrUpdate(tbSysUserOauth);
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete/{id}")
    @AclPermission(code = "system:oauth:delete")
    @ApiOperation(value = "删除第三方账号数据")
    @ApiImplicitParam(name = "id", value = "主键", type = "path")
    public AjaxResponse delete(@PathVariable String id) throws Exception {
        tbSysUserOauthService.delete(id);
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "system:oauth:delete")
    @ApiOperation(value = "删除第三方账号数据")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse deleteBatch() throws Exception {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            tbSysUserOauthService.delete(Convert.toLongArray(ids));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }
}
